/**
 * 
 */
package com.lanswon.qzsmk.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

import com.lanswon.qzsmk.exception.SystemException;

/**
 * 生成采集单条形码
 * @author sun
 *
 */
public class BarcodeUtil {
	private static final String out_cert_Dir_Project = "\\uploadImages\\enclosures\\";
	private static Logger logger = (Logger) LoggerFactory
			.getLogger(BarcodeUtil.class);

	/**
     * 生成文件
     *
     * @param msg 采集单编号
     * @param paperNo 证件号码
     * @return req.getServletContext().getRealPath("/")+
	 * @throws FileNotFoundException 
     */
    public static String generateBarCode(ServletRequest req ,String msg, String paperNo) throws FileNotFoundException {
    	String fileName =  paperNo+"_条形码.png";
    	String path = req.getServletContext().getRealPath("/")+out_cert_Dir_Project+fileName;
        File file = new File(path);
        if(!file.exists()){
        	file.getParentFile().mkdirs();
        	 try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				 String msg1="创建文件异常";
					logger.error(msg1);
					throw new SystemException(SystemException.IOException, msg1);

			}
        }
        try {
            generate(msg, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
        	e.printStackTrace();
        	 String msg1="系统找不到文件异常";
				logger.error(msg1);
				throw new SystemException(SystemException.FileNotFoundException, msg1);

        }
        return path;
    }
    
    /**
     * 生成字节
     *
     * @param msg
     * @return
     */
    private static byte[] generate(String msg) {
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        generate(msg, ous);
        return ous.toByteArray();
    }
    
    /**
     * 生成到流
     *
     * @param msg
     * @param ous
     */
    private static void generate(String msg, OutputStream ous) {
        if (StringUtils.isEmpty(msg) || ous == null) {
            return;
        }
 
        Code39Bean bean = new Code39Bean();
 
        // 精细度
        final int dpi = 150;
        // module宽度
        final double moduleWidth = UnitConv.in2mm(1.0f / dpi);
 
        // 配置对象
        bean.setModuleWidth(moduleWidth);
        bean.setWideFactor(3);
        bean.doQuietZone(false);
 
        String format = "image/png";
        try {
 
            // 输出到流
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(ous, format, dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);
 
            // 生成条形码
            bean.generateBarcode(canvas, msg);
 
            // 结束绘制
            canvas.finish();
        } catch (IOException e) {
        	e.printStackTrace();
        	 String msg1="IO流异常";
				logger.error(msg1);
				throw new SystemException(SystemException.IOException, msg1);

        }
    }
 
    public static void main(String[] args) {
        String msg = "152489548258";
        String path = "d:\\barcode.png";
//        generateBarCode(req, msg, paperNo)(msg, path);
    }
}
